package com.demo.dao;

import com.demo.common.StringUtils;
import com.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/18 9:52
 */
@Repository
@Slf4j
public class UserDao {
    @Autowired
    private Connection conn ;

    public int insert(User user) {
        /**
         * SQL 逻辑
         */
        String sql = "INSERT INTO tb_user (name, password, salt, create_time, gender) VALUES" +
                "(?, ?, ?, ? ,?)" ;
        try (
                PreparedStatement ps = this.conn.prepareStatement(sql)
        ) {
            ps.setString(1, user.getName()) ;
            ps.setString(2, StringUtils.md5Encoder(user.getPassword(),user.getSalt())) ;
            ps.setDate(4, new Date(user.getCreateTime().getTime()));
            ps.setString(3, user.getSalt()) ;
            ps.setInt(5, user.getGender()) ;
            return ps.executeUpdate() ;
        } catch (SQLException e) {
            log.error(e.getMessage()) ;
        }
        return 0 ;
    }
}
